'''
Created on May 7, 2011

@author: yc30v1m
'''
from library.AutoLoad import AutoLoad
from logging import getLogger
from library.Utils import singleton
import string

log = getLogger(__name__)

@singleton
class Aggregator(object):
    '''
    classdocs
    '''
    _testCases = {}
    
    __confReader = ""
    #Fully Qualified Directory for Listners
    __fileFQD = 'library.testcases'

    def __init__(self):
        try:
            self.__confReader = AutoLoad.ClassAsObject('library.Configuration')
            self.__aggregateFromTestCaseProviders()
        except:
            AutoLoad.logExceptionInfo()
        
    def getAllTestCases(self):
        return self._testCases
        
    ''' 
    filter the aggregated test cases, irrespective of the Test cases comming from
    as we have already aggregated the test cases from Configured inputs
    We need to filter the test cases based on there config data
    ''' 
    def getSpecificTestCaseByFileName(self,filename,colname = 'FILE_NAME'):
        filteredTCs = {}
        _filename=filename[string.rfind(filename,'\\')+1:]
        for tcid in self._testCases:
            try:
                _filename = _filename.lower()
                _filename = _filename.strip()
                currentTestCaseFileName = self._testCases[tcid][colname]
                currentTestCaseFileName = currentTestCaseFileName.lower()
                currentTestCaseFileName = currentTestCaseFileName.strip()
                #Check whether the file name matches from test cases
                if currentTestCaseFileName == _filename:
                    filteredTCs[tcid] = self._testCases[tcid]
                    filteredTCs[tcid]['FULL_FILE_NAME'] = filename
                    log.debug('filtered TC added is : ' + tcid)
                #End if
            except:
                log.critical("Unable to Filter Testcase with File name : " + tcid)
                AutoLoad.logExceptionInfo()
        # End For
        return filteredTCs
        
    def getSpecificTestCasesByFileNames(self,fileNamesAssoc,colname = 'FILE_NAME'):
        
        ttcases = {}
        for fileName,fullFileName in fileNamesAssoc.iteritems():
            tcases = self.getSpecificTestCaseByFileName(fullFileName, colname)
            ttcases = self.genericMerge(tcases, ttcases)
        
        return ttcases
        
    def __aggregateFromTestCaseProviders(self):
        aggregatorsStr = self.__confReader.getOptionFromSection('testcases', 'testcases')
        providers = aggregatorsStr.split(",")
        for aggregatorType in providers:
            try:
                straggregator = str(aggregatorType)
                log.debug("Loading Aggregator Type :" + straggregator)
                setupInfo = self.__confReader.getSection('testcases_' + straggregator)
                classStr = AutoLoad.ClassOnly(self.__fileFQD + str(".") + straggregator.capitalize())
                obj = classStr(setupInfo)
                tcs = obj.getTestCases()
                if len(tcs) > 0:
                    self.merge(tcs)
            except:
                log.critical("TestCase Load Failed :" + straggregator)
                AutoLoad.logExceptionInfo()
        
    def merge(self,testCases):
        for k,v in testCases.iteritems():
            if k in self._testCases:
                self._testCases[k] = v
            else:
                self._testCases[k] = v
        return self._testCases
    
    def genericMerge(self,inArr, returnArr={}):
        for tc,tcd in inArr.iteritems():
            if tc in inArr:
                returnArr[tc] = tcd
            else:
                returnArr[tc] = tcd
        return returnArr